package com.meihua.shellapp.fragment;


import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.aspsine.irecyclerview.IRecyclerView;
import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.ClassroomKnowledgeActivity;
import com.meihua.shellapp.adapter.ClassroomrvAdapter;
import com.meihua.shellapp.adapter.OnItemClickListener;
import com.meihua.shellapp.base.BaseRecyclerViewFragment;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.ClassRoomBean;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.common.NetHelper;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;

/**
 * 创客中心 微课堂
 */
public class ClassroomFragment extends BaseRecyclerViewFragment implements OnItemClickListener {
    @Bind(R.id.ins_iRecyclerView)
    IRecyclerView mRecyclerView;
    private ClassroomrvAdapter rvAdapter;
    private List<ClassRoomBean.DataBean> mData;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_classroom;
    }

    @Override
    protected void initView() {
        /*不需要bainner*/
        initRecyclerView(mRecyclerView, false);
        mRecyclerView.setRefreshing(false);
        mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.GONE);
    }

    /*条目点击*/
    @Override
    public void onItemClick(int position, View v) {
        Intent intent = new Intent(getActivity(), ClassroomKnowledgeActivity.class);
        intent.putExtra("type", mData.get(position).getType());
        startActivity(intent);
    }

    @Override
    protected RecyclerView.Adapter getAdapter() {
        mData = new ArrayList();
        rvAdapter = new ClassroomrvAdapter(mData);
        rvAdapter.setOnItemClickListener(this);
        return rvAdapter;
    }

    @Override
    protected void getServiceData(int mPage) {
        mData.clear();
        mRecyclerView.setRefreshing(false);
        /*请求服务器数据*/
        Map<String, String> para = new HashMap();
        NetHelper.post(URL.getUrl().WKT, para, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(Response<String> response) {
                ClassRoomBean bean = MyApplication.getInstance().getBeanForJson(ClassRoomBean.class, response.body());
                if (bean == null) {
                    return;
                }
                if (bean.getCode() == 1) {
                    mData.addAll(bean.getData());
                    rvAdapter.notifyDataSetChanged();
                }
            }
        }, false);
    }

    /*加载更多*/
    @Override
    protected void loadMore() {
        mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.GONE);
    }

    /*刷新*/
    @Override
    protected void refresh() {
        mRecyclerView.setRefreshing(false);
    }
}